home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / demos / GL / flip / swirl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  3.0 KB  |  133 lines

  1. /*
  2.  * Copyright 1991, 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17. /*
  18.  *    A couple of routines that make swirl work
  19.  */
  20. #include "flip.h"
  21.  
  22. #define SECTIONS 19
  23.  
  24. #define SWIRLINC 40
  25.  
  26. #define PolyOrLine(fl)    if (fl == POLYGONS) { \
  27.                 bgnpolygon(); \
  28.             } else { \
  29.                 bgnclosedline(); \
  30.             }
  31.  
  32. #define EndPolyOrLine(fl) if (fl == POLYGONS) { \
  33.                 endpolygon(); \
  34.             } else { \
  35.                 endclosedline(); \
  36.             }
  37. /*
  38.  *    How does this work?  It draws SECTIONS sections of polygons,
  39.  * rotating between drawing them, the magnitude of the rotation
  40.  * defined by how big the 'swirl' paramater is.
  41.  */
  42. draw_swirl(obj)
  43. flipobj *obj;
  44. {
  45.     register int i;
  46.     register float *p, *end;
  47.     int ixr, iyr;
  48.  
  49.     if (obj->swirl > 0)
  50.     {
  51.         srand(0);
  52.         for (i = 0 ; i < SECTIONS ; i++)
  53.         {
  54.             ixr = rand() % 3600;
  55.             iyr = rand() % 3600;
  56.  
  57.             pushmatrix();
  58.             rotate(ixr, 'x');
  59.             rotate(iyr, 'y');
  60.             rotate(obj->swirl, 'x');
  61.             rotate(obj->swirl, 'y');
  62.             rotate(-iyr, 'y');
  63.             rotate(-ixr, 'x');
  64.             p = obj->swirldata + (32*i) ;
  65.             end = obj->swirldata + 8*obj->npoints ;
  66.  
  67.             while (p < end)
  68.             {
  69.                 PolyOrLine(obj->type);
  70.                 n3f(p);
  71.                 v3f(p+4);
  72.                 n3f(p+8);
  73.                 v3f(p+12);
  74.                 n3f(p+16);
  75.                 v3f(p+20);
  76.                 n3f(p+24);
  77.                 v3f(p+28);
  78.                 EndPolyOrLine(obj->type);
  79.                 p += (32*SECTIONS) ;
  80.             }
  81.             popmatrix();
  82.         }
  83.     }
  84.     else
  85.     {
  86.         drawflipobj(obj);
  87.     }
  88.     obj->swirl += SWIRLINC;
  89.     if (obj->swirl > 3600)
  90.     {
  91.         obj->swirl = -60 * SWIRLINC + 1 ;
  92. /* Note:  add 1 above so that obj->swirl doesn't hit zero.  Start at
  93.  * negative so there is a time when it isn't swirling.
  94.  */
  95.     }
  96. }
  97.  
  98. swirl_randomize(obj)
  99. flipobj *obj;
  100. {
  101.     int        i, j;
  102.     int        *ip, *ip1;
  103.     int        temp[32];
  104.     int        *t1, *t2;
  105.     int        off1, off2;
  106.  
  107.     /*
  108.      *    Randomize the polygons for swirl.
  109.      */
  110.  
  111.     /* First, copy data over... */
  112.     obj->swirldata = (float *)malloc(sizeof(float) * 8 * obj->npoints);
  113.     memcpy(obj->swirldata, obj->data, sizeof(float) * 8 * obj->npoints);
  114.  
  115.     for (i = 0; i < obj->npoints / 8; i++)
  116.     {
  117.         off1 =(rand() % (obj->npoints / 4)) * 32;
  118.         off2 =(rand() % (obj->npoints / 4)) * 32;
  119.         ip = ip1 = (int *)obj->swirldata;
  120.         ip += off1;
  121.         ip1 += off2;
  122.         if (ip != ip1)
  123.         {
  124.             for (t1 = temp, t2 = ip, j=0; j < 32; j++)
  125.             *t1++ = *t2++;
  126.             for (t1 = ip, t2 = ip1, j=0; j < 32; j++)
  127.             *t1++ = *t2++;
  128.             for (t1 = ip1, t2 = temp, j=0; j < 32; j++)
  129.             *t1++ = *t2++;
  130.         }
  131.     }
  132. }
  133.